
package cn.wjdiankong.levideo.fragment;

import java.util.Map;

import com.ss.android.common.applog.GlobalContext;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.kuaishou.data.KuaishouVideoListData;
import cn.wjdiankong.levideo.kuaishou.utils.KuaishouUtils;
import cn.wjdiankong.levideo.list.adapter.KuaishouFragmentListAdapter;
import cn.wjdiankong.levideo.net.HttpClientUtils;
import cn.wjdiankong.levideo.net.HttpClientUtils.ResponseCallback;
import cn.wjdiankong.levideo.utils.Logger;
import cn.wjdiankong.levideo.utils.PreferenceUtil;
import cn.wjdiankong.levideo.view.StaggeredGridView;

public class KuaishouFragment extends BaseFragment{

	private static final String ARG_POSITION = "position";
	
	private int page = 1;
	private int refreshTime = 0;
	private final String PREF_KEY_REFRESHTIME = "refresh_time";
	
	public static KuaishouFragment newInstance(int position, String title, int type) {
		KuaishouFragment f = new KuaishouFragment();
		f.title = title;
		Bundle b = new Bundle();
		b.putInt(ARG_POSITION, position);
		f.setArguments(b);
		f.type = type;
		return f;
	}

	@SuppressLint({ "InlinedApi", "InflateParams", "NewApi" })
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View layout = super.onCreateView(inflater, container, savedInstanceState);
		mListView = (StaggeredGridView) layout.findViewById(R.id.grid_view);
		refreshTime = (int)PreferenceUtil.getValue(PREF_KEY_REFRESHTIME, 0);
		adapter = new KuaishouFragmentListAdapter(getActivity(), mListView, isLocal);
		adapter.setListView(mListView);
		initListViewListener();
		if(!isLocal){
			showLoading();
			getBeforeListData();
		}else{
			if(adapter != null){
				adapter.setBeforeDataList(videoDataList);
			}
		}
		return layout;
	}
	
	public void getBeforeListData(){
		refreshTime++;
		PreferenceUtil.setValue(PREF_KEY_REFRESHTIME, refreshTime);
		
		Map<String, String> bodyMap = KuaishouUtils.getBodyParams(1, refreshTime);
		Map<String, String> paramsMap = KuaishouUtils.getCommonParams();
		String sig = KuaishouUtils.getSign(GlobalContext.getContext(), paramsMap, bodyMap);
		if(!TextUtils.isEmpty(sig)){
			bodyMap.put("sig", sig);
		}
		String url = KuaishouUtils.getUrl();
		HttpClientUtils httpClient = new HttpClientUtils();
		httpClient.setUrl(url).setParams(bodyMap).setResponseCallback(new ResponseCallback(){
			@Override
			public void responseFinish(final String result) {
				KuaishouVideoListData listData = KuaishouVideoListData.fromJSON(result);
				loadDownDataFinish(listData.videoDataList);
			}

			@Override
			public void responseFinish(byte[] result) {
			}

			@Override
			public void reponseFail() {
				Logger.log("get data err!");
				loadDataFailed();
			}

		}).execPost();
	}

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_kuaishou_layout;
	}

	@Override
	protected void getAfterListData() {
		page++;
		refreshTime++;
		PreferenceUtil.setValue(PREF_KEY_REFRESHTIME, refreshTime);
		
		Map<String, String> bodyMap = KuaishouUtils.getBodyParams(page, refreshTime);
		Map<String, String> paramsMap = KuaishouUtils.getCommonParams();
		String sig = KuaishouUtils.getSign(GlobalContext.getContext(), paramsMap, bodyMap);
		if(!TextUtils.isEmpty(sig)){
			bodyMap.put("sig", sig);
		}
		String url = KuaishouUtils.getUrl();
		HttpClientUtils httpClient = new HttpClientUtils();
		httpClient.setUrl(url).setParams(bodyMap).setResponseCallback(new ResponseCallback(){
			@Override
			public void responseFinish(final String result) {
				KuaishouVideoListData listData = KuaishouVideoListData.fromJSON(result);
				loadUpDataFinish(listData.videoDataList);
			}

			@Override
			public void responseFinish(byte[] result) {
			}

			@Override
			public void reponseFail() {
				Logger.log("get data err!");
				loadDataFailed();
			}

		}).execPost();
	}
	
}